local skynet = require "skynet"
local filelog = require "filelog"
local msghelper = require "agenthelper"
local msgproxy = require "msgproxy"
local table = table
local processstate = require "processstate"
local playerdatadao = require "playerdatadao"
local globaldatadao = require "globaldatadao"
local timetool = require "timetool"
local configdao = require "configdao"
require "enum"
local processing = processstate:new({timeout=4})
local  GameRecord = {}

--[[
//请求玩家战绩信息
message PlayerGameRecordinfoReq {
	optional Version version = 1;
	optional int32 rid = 2;
}
//响应玩家战绩信息
message PlayerGameRecordinfoRes {
	optional int32 errcode = 1; //错误原因 0表示成功
	optional string errcodedes = 2; //错误描述
	repeated PlayerGameRecordinfo recordinfo = 3; // 
}
]]

local globalvarcfg = nil

function  GameRecord.process(session, source, fd, request)
    local responsemsg = {
        errcode = EErrCode.ERR_SUCCESS,
    }
    local server = msghelper:get_server()

    --检查当前登陆状态
    if not msghelper:is_login_success() then
        filelog.sys_warning("GameRecord.process invalid server state", server.state)
        responsemsg.errcode = EGateAgentState.ERR_INVALID_REQUEST
        responsemsg.errcodedes = "无效的请求！"
        msghelper:send_resmsgto_client(fd, "PlayerGameRecordinfoRes", responsemsg)
        return
    end

    if processing:is_processing() then
        responsemsg.errcode = EErrCode.ERR_DEADING_LASTREQ
        responsemsg.errcodedes = "正在处理上一次请求！"
        msghelper:send_resmsgto_client(fd, "PlayerGameRecordinfoRes", responsemsg)
        return
    end

    local status
    local records

    processing:set_process_state(true)

    status, records = playerdatadao.query_player_tablerecords(request.rid)
    processing:set_process_state(false)

    if not msghelper:is_login_success() then
        filelog.sys_warning("GameRecord.process invalid server state", server.state)
        responsemsg.errcode = EGateAgentState.ERR_INVALID_REQUEST
        responsemsg.errcodedes = "无效的请求！"
        msghelper:send_resmsgto_client(fd, "PlayerGameRecordinfoRes", responsemsg)
        return
    end

    if status == nil or records == nil then
        responsemsg.errcode = EGateAgentState.ERR_INVALID_REQUEST
        responsemsg.errcodedes = "访问数据失败！"
        msghelper:send_resmsgto_client(fd, "PlayerGameRecordinfoRes", responsemsg)
        return        
    end

    responsemsg.records = {}

    local now_time = timetool.get_time()
    for _, record in ipairs(records) do
        if timetool.get_diffdate_day(record.terminal_time, now_time) >= configdao.get_businessconfitem_by_index(100, 1000, "globalvarcfg", "record_keep_day") then
            globaldatadao.save_tablerecord("delete", record.id, record.gameid, nil)
            playerdatadao.save_player_tablerecords("delete", server.rid, record)            
        else
            table.insert(responsemsg.records, record)
        end
    end
    --[[table.sort(records, function(a,b) return a.terminal_time > b.terminal_time end)
    local value
    local record_keep_num = configdao.get_businessconfitem_by_index(100, 1000, "globalvarcfg", "record_keep_num")
    for i=1, #records do
        value = records[i]
        if i >= record_keep_num then
            globaldatadao.save_tablerecord("delete", value.id, value.gameid, nil)
            playerdatadao.save_player_tablerecords("delete", server.rid, value)
        else
            table.insert(responsemsg.records, value)            
        end
    end]]
    	
    msghelper:send_resmsgto_client(fd, "PlayerGameRecordinfoRes", responsemsg)

end

return GameRecord
